package org.example.managementsystem.repository;

import org.example.managementsystem.model.DeviceRepair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 设备报修数据访问对象
 * @author 王振祎
 * @date 2025-05-21
 */
@Repository
public class DeviceRepairDao {
    private final JdbcTemplate jdbcTemplate;

    @Autowired
    public DeviceRepairDao(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 插入设备报修
     */
    public int insertRepair(DeviceRepair repair) {
        String sql = "INSERT INTO device_repair_application (device_number, repair_desc, applicant_job_number, is_approved, status) " +
                "VALUES (?, ?, ?, ?, ?)";
        jdbcTemplate.update(sql,
                repair.getDeviceNumber(),
                repair.getRepairDesc(),
                repair.getApplicantJobNumber(),
                repair.getIsApproved(),
                repair.getStatus());
        // 获取刚刚插入的 id
        String getLastIdSql = "SELECT LAST_INSERT_ID()";
        int lastInsertId = jdbcTemplate.queryForObject(getLastIdSql, Integer.class);
        return lastInsertId;
    }

    /**
     * 更新设备报修状态
     */
    public int updateRepairStatus(int repairId, boolean isApproved, String description) {
        String sql = "UPDATE device_repair_application SET is_approved = ?, repair_desc = ? WHERE repair_id = ?";
        return jdbcTemplate.update(sql, isApproved, description, repairId);
    }

    public int updateRepairStatus(int repairId, boolean isApproved, boolean status) {
        String sql = "UPDATE device_repair_application SET is_approved = ?, status = ? WHERE repair_id = ?";
        return jdbcTemplate.update(sql, isApproved, status, repairId);
    }

    public int updateRepairStatus(int repairId, boolean isApproved) {
        String sql = "UPDATE device_repair_application SET is_approved = ? WHERE repair_id = ?";
        return jdbcTemplate.update(sql, isApproved, repairId);
    }

    public int updateRepair(DeviceRepair repair){
        String sql = "UPDATE device_repair_application SET device_number = ?, repair_desc = ?, is_approved = ?, status = ? WHERE repair_id = ?";
        return jdbcTemplate.update(sql, repair.getDeviceNumber(), repair.getRepairDesc(), repair.getIsApproved(), repair.getStatus(), repair.getRepairId());
    }

    /**
     * 查询所有报修
     */
    public List<DeviceRepair> getAllRepairs() {
        String sql = "SELECT * FROM device_repair_application";
        return jdbcTemplate.query(sql, new DeviceRepairRowMapper());
    }

    /**
     * 查询所有未审核的报修
     */
    public List<DeviceRepair> getAllUnreviewedRepairs() {
        String sql = "SELECT * FROM device_repair_application WHERE status = 0";
        return jdbcTemplate.query(sql, new DeviceRepairRowMapper());
    }

    /**
     * 根据ID查询报修
     */
    public DeviceRepair getRepairById(int repairId) {
        String sql = "SELECT * FROM device_repair_application WHERE repair_id = ?";
        List<DeviceRepair> repairs = jdbcTemplate.query(sql, new DeviceRepairRowMapper(), repairId);
        return repairs.isEmpty() ? null : repairs.get(0);
    }

    /**
     * 根据用户ID查询报修记录
     */
    public List<DeviceRepair> getRepairByUserId(int userId) {
        String sql = "SELECT * FROM device_repair_application WHERE applicant_job_number = ?";
        return jdbcTemplate.query(sql, new DeviceRepairRowMapper(), userId);
    }

    /**
     * 根据设备ID查询报修记录
     */
    public List<DeviceRepair> getRepairByDeviceId(int deviceId) {
        String sql = "SELECT * FROM device_repair_application WHERE device_number = ?";
        return jdbcTemplate.query(sql, new DeviceRepairRowMapper(), deviceId);
    }

    /**
     * 报修行映射器
     */
    private static class DeviceRepairRowMapper implements RowMapper<DeviceRepair> {
        @Override
        public DeviceRepair mapRow(ResultSet rs, int rowNum) throws SQLException {
            DeviceRepair repair = new DeviceRepair();
            repair.setRepairId(rs.getInt("repair_id"));
            repair.setDeviceNumber(rs.getString("device_number"));
            repair.setRepairDesc(rs.getString("repair_desc"));
            repair.setApplicantJobNumber(rs.getString("applicant_job_number"));
            repair.setIsApproved(rs.getBoolean("is_approved"));
            repair.setStatus(rs.getBoolean("status"));

            
            return repair;
        }
    }

    /**
     * 根据ID删除报修
     */
    public int deleteRepair(int repairId) {
        String sql = "DELETE FROM device_repair_application WHERE repair_id = ?";
        return jdbcTemplate.update(sql, repairId);
    }
}